#include		"ModuleServer.h"

ModuleServer::ModuleServer(Processor &proc) : _proc(proc)
{
	_operator[LOGIN] = &ModuleServer::login;
	_operator[LIST] = &ModuleServer::list;
	_operator[WHO] = &ModuleServer::who;
	_operator[PONG] = &ModuleServer::pong;
}

ModuleServer::~ModuleServer()
{

}

void									ModuleServer::takeOrder(IOrder *order, Protocol proto)
{
	byte	op = *(byte*)order->getData(sizeof(byte));

	std::cout << "Operator" << (Operator) op << std::endl;
	if (_operator.find((Operator) op) != _operator.end())
		(this->*_operator[(Operator) op])(order);
	delete order;
}

void									ModuleServer::login(IOrder *order)
{
	std::cout << "size of msg = " << order->size() << std::endl;
	byte	loglen = *(byte*)order->getData(sizeof(byte));
	std::string  login((char*)order->getData(loglen), loglen);
	byte	passlen = *(byte*)order->getData(sizeof(byte));
	std::string	 pass((char*)order->getData(passlen), passlen);

	std::cout << (unsigned int) loglen << std::endl;
	std::cout << (unsigned int) passlen << std::endl;
	std::cout << login << std::endl;
	std::cout << pass << std::endl;
	std::string			ip = _proc.getIp(order->getSenderId());

	Client	*client = new Client(order->getSenderId(), ip);
	client->setName(login);
	_client.addClient(order->getSenderId(), client);
}

void									ModuleServer::pong(IOrder *order)
{
	std::cout << "received PONG" << std::endl;
}

void									ModuleServer::who(IOrder *order)
{
	unsigned int	id = *(unsigned int*)order->getData(sizeof(unsigned int));
	Protocol	proto = SERVER;
	Operator	op = WHO;
	int			cnt = 0;

	if (_client[id] != NULL)
	{
		IOrder		*newOrder = new Order();
		newOrder->init();
		newOrder->pushData(&proto, sizeof(proto));
		newOrder->pushData(&op, sizeof(op));
		newOrder->pushData(&id, sizeof(id));
		std::cout << "ip : " << _client[id]->getIp() << std::endl;
		std::string			ip = _client[id]->getIp();
		std::istringstream	iss;
		int					cip;
		char				tmp;
		while (ip.find(".") != -1)
		{
			iss.clear();
			iss.str(ip.substr(0, ip.find(".")));
			ip = ip.substr(ip.find(".") + 1, ip.size());
			iss >> cip;
			tmp = cip;
			newOrder->pushData(&tmp, sizeof(tmp));
		}
		iss.clear();
		iss.str(ip);
		iss >> cip;
		tmp = cip;
		newOrder->pushData(&tmp, sizeof(tmp));
		unsigned char	loglen = sizeof(_client[id]->getName().c_str());
		newOrder->pushData(&loglen, sizeof(loglen));
		newOrder->pushData((char *) _client[id]->getName().c_str(), loglen);
		_proc.sendOrder(newOrder, order->getSenderId());
	}
}

void									ModuleServer::list(IOrder *order)
{
	Protocol							proto = SERVER;
	Operator							op = LIST;
	Client								*client = NULL;
	unsigned short						nbr = _client.size() - 1;

	IOrder		*newOrder = new Order;
	newOrder->init();
	newOrder->pushData(&proto, sizeof(proto));
	newOrder->pushData(&op, sizeof(op));
	newOrder->pushData(&nbr, sizeof(nbr));
	std::cout << "nbr client " << nbr << std::endl;
	_client.start();
	while ((client = _client.getNextClient()) != NULL)
	{
		unsigned int	id = client->getId();
		if (id != order->getSenderId())
			newOrder->pushData(&id, sizeof(id));
	}
	_proc.sendOrder(newOrder, order->getSenderId());
}

std::vector<Protocol>					ModuleServer::getProtocol() const
{
	return _proto;
}

void									ModuleServer::init()
{

}
